#include <common.h>
#include <malloc.h>
#include <spi.h>

#define SLEEPMSEC		0x1000
#define ENDDEF			0x2000
#define	DEFMASK			0xFF00
#define COMMAND_ONLY		0xFE
#define DATA_ONLY		0xFF

#define MAX_GAMMA_LEVEL		5
#define GAMMA_TABLE_COUNT	21

#define MIN_BRIGHTNESS		0
#define MAX_BRIGHTNESS		255
#define DEFAULT_BRIGHTNESS	150

struct tl2796_controller {
	struct spi_slave *priv;
	unsigned char brightness;
};

static const unsigned short seq_display_on[] = {
	0x14, 0x03,
	ENDDEF, 0x0000
};

static const unsigned short seq_display_off[] = {
	0x14, 0x00,
	ENDDEF, 0x0000
};

static const unsigned short seq_stand_by_on[] = {
	0x1D, 0xA1,
	SLEEPMSEC, 200,
	ENDDEF, 0x0000
};

static const unsigned short seq_stand_by_off[] = {
	0x1D, 0xA0,
	SLEEPMSEC, 250,
	ENDDEF, 0x0000
};

static const unsigned short seq_setting[] = {
	0x31, 0x08,
	0x32, 0x14,
	0x30, 0x02,
	0x27, 0x01,
	0x12, 0x08,
	0x13, 0x08,
	0x15, 0x03,
	0x16, 0x00,

	0xef, 0xd0,
	DATA_ONLY, 0xe8,

	0x39, 0x44,
	0x40, 0x00,
	0x41, 0x3f,
	0x42, 0x2a,
	0x43, 0x27,
	0x44, 0x27,
	0x45, 0x1f,
	0x46, 0x44,
	0x50, 0x00,
	0x51, 0x00,
	0x52, 0x17,
	0x53, 0x24,
	0x54, 0x26,
	0x55, 0x1f,
	0x56, 0x43,
	0x60, 0x00,
	0x61, 0x3f,
	0x62, 0x2a,
	0x63, 0x25,
	0x64, 0x24,
	0x65, 0x1b,
	0x66, 0x5c,

	0x17, 0x22,
	0x18, 0x33,
	0x19, 0x03,
	0x1a, 0x01,
	0x22, 0xa4,
	0x23, 0x00,
	0x26, 0xa0,

	0x1d, 0xa0,
	SLEEPMSEC, 3000,

	0x14, 0x03,

	ENDDEF, 0x0000
};

/* gamma value: 2.2 */
static const unsigned int ams369fg06_22_250[] = {
	0x00, 0x3f, 0x2a, 0x27, 0x27, 0x1f, 0x44,
	0x00, 0x00, 0x17, 0x24, 0x26, 0x1f, 0x43,
	0x00, 0x3f, 0x2a, 0x25, 0x24, 0x1b, 0x5c,
};

static const unsigned int ams369fg06_22_200[] = {
	0x00, 0x3f, 0x28, 0x29, 0x27, 0x21, 0x3e,
	0x00, 0x00, 0x10, 0x25, 0x27, 0x20, 0x3d,
	0x00, 0x3f, 0x28, 0x27, 0x25, 0x1d, 0x53,
};

static const unsigned int ams369fg06_22_150[] = {
	0x00, 0x3f, 0x2d, 0x29, 0x28, 0x23, 0x37,
	0x00, 0x00, 0x0b, 0x25, 0x28, 0x22, 0x36,
	0x00, 0x3f, 0x2b, 0x28, 0x26, 0x1f, 0x4a,
};

static const unsigned int ams369fg06_22_100[] = {
	0x00, 0x3f, 0x30, 0x2a, 0x2b, 0x24, 0x2f,
	0x00, 0x00, 0x00, 0x25, 0x29, 0x24, 0x2e,
	0x00, 0x3f, 0x2f, 0x29, 0x29, 0x21, 0x3f,
};

static const unsigned int ams369fg06_22_50[] = {
	0x00, 0x3f, 0x3c, 0x2c, 0x2d, 0x27, 0x24,
	0x00, 0x00, 0x00, 0x22, 0x2a, 0x27, 0x23,
	0x00, 0x3f, 0x3b, 0x2c, 0x2b, 0x24, 0x31,
};

struct ams369fg06_gamma {
	unsigned int *gamma_22_table[MAX_GAMMA_LEVEL];
};

static struct ams369fg06_gamma gamma_table = {
	.gamma_22_table[0] = (unsigned int *)&ams369fg06_22_50,
	.gamma_22_table[1] = (unsigned int *)&ams369fg06_22_100,
	.gamma_22_table[2] = (unsigned int *)&ams369fg06_22_150,
	.gamma_22_table[3] = (unsigned int *)&ams369fg06_22_200,
	.gamma_22_table[4] = (unsigned int *)&ams369fg06_22_250,
};

static int tl2796_spi_read_byte(struct tl2796_controller *tl2796, int addr, int index)
{
	struct spi_slave *spi = tl2796->priv;
	u8 tbuf[2];
	u8 rbuf[2];

	spi_claim_bus(spi);
	spi_cs_activate(spi);

	tbuf[0] = (u8)addr;
	tbuf[1] = (u8)index;
	spi_xfer(spi, sizeof(tbuf) * 8, tbuf, rbuf, SPI_XFER_END);

	return rbuf[1];
}

static int tl2796_spi_write_byte(struct tl2796_controller *tl2796, int addr, int data)
{
	struct spi_slave *spi = tl2796->priv;
	u8 tbuf[2];
	u8 rbuf[2];

	spi_claim_bus(spi);
	spi_cs_activate(spi);

	tbuf[0] = (u8)addr;
	tbuf[1] = (u8)data;
	spi_xfer(spi, sizeof(tbuf) * 8, tbuf, rbuf, SPI_XFER_END);

	return 0;
}

static int tl2796_spi_write(struct tl2796_controller *tl2796, unsigned char address,
	unsigned char command)
{
	int ret = 0;

	if (address != DATA_ONLY)
		ret = tl2796_spi_write_byte(tl2796, 0x70, address);
	if (command != COMMAND_ONLY)
		ret = tl2796_spi_write_byte(tl2796, 0x72, command);

	return ret;
}

static int tl2796_panel_send_sequence(struct tl2796_controller *tl2796,
	const unsigned short *wbuf)
{
	int ret = 0, i = 0;

	while ((wbuf[i] & DEFMASK) != ENDDEF) {
		if ((wbuf[i] & DEFMASK) != SLEEPMSEC) {
			ret = tl2796_spi_write(tl2796, wbuf[i], wbuf[i+1]);
			if (ret)
				break;
		} else {
			udelay(wbuf[i+1]);
		}
		i += 2;
	}

	return ret;
}

static int _tl2796_gamma_ctl(struct tl2796_controller *tl2796,
	const unsigned int *gamma)
{
	unsigned int i = 0;
	int ret = 0;

	for (i = 0 ; i < GAMMA_TABLE_COUNT / 3; i++) {
		ret = tl2796_spi_write(tl2796, 0x40 + i, gamma[i]);
		ret = tl2796_spi_write(tl2796, 0x50 + i, gamma[i+7*1]);
		ret = tl2796_spi_write(tl2796, 0x60 + i, gamma[i+7*2]);
		if (ret) {
			printf("failed to set gamma table.\n");
			goto gamma_err;
		}
	}

gamma_err:
	return ret;
}

static int tl2796_gamma_ctl(struct tl2796_controller *tl2796, int brightness)
{
	int ret = 0;
	int gamma = 0;

	if ((brightness >= 0) && (brightness <= 50))
		gamma = 0;
	else if ((brightness > 50) && (brightness <= 100))
		gamma = 1;
	else if ((brightness > 100) && (brightness <= 150))
		gamma = 2;
	else if ((brightness > 150) && (brightness <= 200))
		gamma = 3;
	else if ((brightness > 200) && (brightness <= 255))
		gamma = 4;

	ret = _tl2796_gamma_ctl(tl2796, gamma_table.gamma_22_table[gamma]);

	return ret;
}

static int tl2796_ldi_init(struct tl2796_controller *tl2796)
{
	int ret, i;
	static const unsigned short *init_seq[] = {
		seq_setting,
		seq_stand_by_off,
	};

	for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
		ret = tl2796_panel_send_sequence(tl2796, init_seq[i]);
		if (ret)
			break;
	}

	return ret;
}

static int tl2796_ldi_enable(struct tl2796_controller *tl2796)
{
	int ret, i;
	static const unsigned short *init_seq[] = {
		seq_stand_by_off,
		seq_display_on,
	};

	for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
		ret = tl2796_panel_send_sequence(tl2796, init_seq[i]);
		if (ret)
			break;
	}

	return ret;
}

void tl2796_read_id(struct tl2796_controller *tl2796)
{
	int id;
	tl2796_spi_write_byte(tl2796, 0x70, 0x00);
	id = tl2796_spi_read_byte(tl2796, 0x71, 0x00);
	tl2796_spi_write_byte(tl2796, 0x70, 0x01);
	id = (id << 8) | tl2796_spi_read_byte(tl2796, 0x71, 0x01);

	tl2796_spi_write_byte(tl2796, 0x70, 0x02);
	id = (id << 8) | tl2796_spi_read_byte(tl2796, 0x71, 0x02);
	printf("tl2796 id:%x\r\n", id);
}

int tl2796_init(void)
{
	int ret = 0;
	struct tl2796_controller tl2796;

	tl2796.priv = spi_setup_slave(0, 3, 1200000, SPI_MODE_3);
	tl2796.brightness = DEFAULT_BRIGHTNESS;

	tl2796_read_id(&tl2796);

	ret = tl2796_ldi_init(&tl2796);
	if (ret) {
		printf("failed to initialize ldi.\n");
		return ret;
	}

	ret = tl2796_ldi_enable(&tl2796);
	if (ret) {
		printf("failed to enable ldi.\n");
		return ret;
	}

	/* set brightness to current value after power on or resume. */
	ret = tl2796_gamma_ctl(&tl2796, tl2796.brightness);
	if (ret) {
		printf("lcd gamma setting failed.\n");
		return ret;
	}

	return 0;
}
